package com.mars.module.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mars.common.response.PageInfo;
import com.mars.framework.context.ContextUserInfoThreadHolder;
import com.mars.module.system.mapper.SysUserMapper;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import com.mars.module.admin.request.SysUserCaseInformationRequest;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import com.mars.module.admin.mapper.SysUserCaseInformationMapper;
import org.springframework.beans.BeanUtils;
import com.mars.module.admin.entity.SysUserCaseInformation;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.mars.module.admin.service.ISysUserCaseInformationService;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 患者病例信息业务层处理
 *
 * @author mars
 * @date 2024-04-11
 */
@Slf4j
@Service
@AllArgsConstructor
public class SysUserCaseInformationServiceImpl implements ISysUserCaseInformationService {

    private final SysUserCaseInformationMapper baseMapper;
    private final SysUserMapper userMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public SysUserCaseInformation add(SysUserCaseInformationRequest request) {
        SysUserCaseInformation entity = SysUserCaseInformation.builder().build();
        BeanUtils.copyProperties(request, entity);
        entity.setUserId(request.getUserId());
        Long userId = ContextUserInfoThreadHolder.get().getId();
        entity.setCreateById(userId);
        baseMapper.insert(entity);
        return entity;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean delete(Long id) {
        return baseMapper.deleteById(id) > 0;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deleteBatch(List<Long> ids) {
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean update(SysUserCaseInformationRequest request) {
        SysUserCaseInformation entity = SysUserCaseInformation.builder().build();
        BeanUtils.copyProperties(request, entity);
        return baseMapper.updateById(entity) > 0;
    }

    @Override
    public SysUserCaseInformation getById(Long id) {
        return baseMapper.selectById(id);
    }

    @Override
    public PageInfo<SysUserCaseInformation> pageList(SysUserCaseInformationRequest request) {
        Page<SysUserCaseInformation> page = new Page<>(request.getPageNo(), request.getPageSize());
        LambdaQueryWrapper<SysUserCaseInformation> query = this.buildWrapper(request);
        IPage<SysUserCaseInformation> pageRecord = baseMapper.selectPage(page, query);
        List<SysUserCaseInformation> records = pageRecord.getRecords();
        List<SysUserCaseInformation> recordsNew = new ArrayList<>();
        for (SysUserCaseInformation information : records) {
            information.setUserName(userMapper.selectById(information.getUserId()).getUserName());
            recordsNew.add(information);
        }
        if (StringUtils.isNotBlank(request.getUserName())) {
            recordsNew = recordsNew.stream().filter(e -> e.getUserName().contains(request.getUserName())).collect(Collectors.toList());
        }
        pageRecord.setRecords(recordsNew);
        return PageInfo.build(pageRecord);
    }


    @Override
    public List<SysUserCaseInformation> list(SysUserCaseInformationRequest request) {
        LambdaQueryWrapper<SysUserCaseInformation> query = this.buildWrapper(request);
        return baseMapper.selectList(query);
    }

    private LambdaQueryWrapper<SysUserCaseInformation> buildWrapper(SysUserCaseInformationRequest param) {
        LambdaQueryWrapper<SysUserCaseInformation> query = new LambdaQueryWrapper<>();
        if (StringUtils.isNotBlank(param.getMedicalHistory())) {
            query.like(SysUserCaseInformation::getMedicalHistory, param.getMedicalHistory());
        }
        if (StringUtils.isNotBlank(param.getCurrentMedicalHistory())) {
            query.like(SysUserCaseInformation::getCurrentMedicalHistory, param.getCurrentMedicalHistory());
        }
        if (StringUtils.isNotBlank(param.getDisease())) {
            query.like(SysUserCaseInformation::getDisease, param.getDisease());
        }
        if (StringUtils.isNotBlank(param.getSign())) {
            query.like(SysUserCaseInformation::getSign, param.getSign());
        }
        return query;
    }

}
